//
//  LGSSelectNodeTypeSheetController.m
//  LogicSim
//
//  Created by Jeremy Pereira on 29/04/2012.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "LGSConnectNodeSheetController.h"
#import "LGSConnectNodeSelectorDelegate.h"
#import "LGSName.h"

enum  
{
    SHEET_END_OK = 0,
    SHEET_END_CANCEL = 1,
};

@interface LGSConnectNodeSheetController()

@property (weak) id<LGSConnectNodeSelectorDelegate> delegate;

-(void) populateConnections: (NSPopUpButton*) connections
            forNodeSelector: (NSPopUpButton*) nodeSelector
                  useInputs: (bool) useInputs;

@end

@implementation LGSConnectNodeSheetController

@synthesize sourceNodeSelector, destNodeSelector;

-(void) setUpUI
{
    [[self sourceNodeSelector] removeAllItems];
    [[self destNodeSelector] removeAllItems];
    for (int i = 0 ; i < [[self delegate] itemCountForNodeSelectorController: self] ; ++i)
    {
        LGSName* theNodeLabel = [[self delegate] nameForNodeController: self
                                                               atIndex: i];
        [[self sourceNodeSelector] addItemWithTitle: [theNodeLabel stringValue]];
        [[self destNodeSelector] addItemWithTitle: [theNodeLabel stringValue]];
    }
    [self populateConnections: [self sourceConnectionSelector]
              forNodeSelector: [self sourceNodeSelector]
     				useInputs: false];
    [self populateConnections: [self destConnectionSelector]
              forNodeSelector: [self destNodeSelector]
                    useInputs: true];
    
}

-(void) populateConnections: (NSPopUpButton*) connections
            forNodeSelector: (NSPopUpButton*) nodeSelector
                  useInputs: (bool) useInputs
{
    [connections removeAllItems];
    NSInteger selectedIndex = [nodeSelector indexOfSelectedItem];
    if (selectedIndex >= 0)
    {
        NSArray* itemNames = nil;
        if (useInputs)
        {
            itemNames = [[self delegate] inputNamesForNodeSelectorController: self
                                                          			 atIndex: selectedIndex];
        }
        else
        {
            itemNames = [[self delegate] outputNamesForNodeSelectorController: self
                                                                      atIndex: selectedIndex];
        }
        for (NSString* item in itemNames)
        {
            [connections addItemWithTitle: item];
        }
    }
    
}

-(void) informDelegateOfEndSheet
{
    [[self delegate] nodeSelector: self
              selectedSourceIndex: [[self sourceNodeSelector] indexOfSelectedItem]
                        destIndex: [[self destNodeSelector] indexOfSelectedItem]
             selectedSourceOutput: [[self sourceConnectionSelector] titleOfSelectedItem]
               selectedDestOutput: [[self destConnectionSelector] titleOfSelectedItem]];
    
}

-(IBAction) changeNodeSelection: (id)sender
{
    if (sender == [self sourceNodeSelector])
    {
        [self populateConnections: [self sourceConnectionSelector]
                  forNodeSelector: [self sourceNodeSelector] useInputs: false];
    }
    else
    {
        [self populateConnections: [self destConnectionSelector]
                  forNodeSelector: [self destNodeSelector] useInputs: true];
    }
}


@end
